<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    protected $current_controller;
    protected $current_resources;
    protected $my_controller;
    var $CSS = null; // add by idin
    var $JS = null;  // add by idin

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('convert');
        $this->resources = $this->session->userdata('my_resource');
        $this->current_controller = $this->uri->segment(1);
        $this->config->set_item('language', 'english');
        // check wrong url 
        if(count($this->uri->segments) < 2 ){
            redirect(base_url().'index.php/'.$this->current_controller.'/home'); 
        }
    }

    private function get_resouces($controller, $items) {
        $items = empty($items) ? array() : $items;
        foreach ($items as $item) {
            if ($controller . "/home" == $item->controller) return $item;
        }
    }


   public function generate_top_menu($controller, $current, $items) {
        $r = ''; $selected = '';
        $r .= "<ul>";
        $items = empty($items) ? array() : $items;
        foreach ($items as $item) {
            if ($item->parent_id == $controller) {
                $arr = explode('/', $item->controller); //
                if ($arr[0] == $current) $selected = "id='current'";
                $r.="<li $selected>" . anchor(base_url() . $item->controller,
                                "<span >" . $item->name . "</span>") . "</li>";
                $selected = '';
            }
        }
        $r .= "</ul>";
        return $r;
    }

    public function generate_left_menu($item) {
       foreach($item as $row){
           
       }
    }

   private function get_page_offset() {
        $offset = 0;
        if (is_numeric($this->uri->segment(3))) {
            $offset = $this->uri->segment(3);
        } else {
            if (is_numeric($this->input->post("offset"))) {
                $offset = $this->input->post("offset");
            } else {
                $offset = 0;
            } 
        } 

        return ($offset);
    }
    
  public function plugColorbox(){
      $this->setJS(base_url()."assets/js/jquery.colorbox.js");
      $this->setCSS(base_url()."assets/css/colorbox.css");
      
  }
  
  public function plugTypeFace(){
       $this->setJS(URLPATH_JS."font/typeface.js");
       $this->setJS(URLPATH_JS."font/psl_kittithada_bold.typeface.js");
       $this->setJS(URLPATH_JS."font/psl_kittithada_regular.typeface.js");
      
  }
  
    

  public  function setCSS($link) {
        $this->CSS .= "<link rel='stylesheet' href='{$link}'/>\n";
        $this->data['CSS'] = $this->CSS;
    }

  public function setJS($link) {
        $this->JS .="<script src='{$link}'></script>\n";
        $this->data['JS'] = $this->JS;
    }

}

?>
